package com.neusoft.neuiotms.dcom.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.SchedulingRuleConverter;
import com.neusoft.neuiotms.dcom.dto.SchedulingRuleDTO;
import com.neusoft.neuiotms.dcom.model.SchedulingRule;
import com.neusoft.neuiotms.dcom.repository.SchedulingRuleRepository;
import com.neusoft.neuiotms.dcom.service.SchedulingRuleService;

@Service
public class SchedulingRuleServiceImpl implements SchedulingRuleService {

    @Autowired
    private SchedulingRuleRepository schedulingRuleRepository;
    @Autowired
    private SchedulingRuleConverter schedulingRuleConverter;

    @Override
    public List<SchedulingRuleDTO> index(final Searchable searchable) {
        final List<SchedulingRule> result = this.schedulingRuleRepository.index(searchable);
        return this.schedulingRuleConverter.toListDTO(result);
    }

    @Override
    public Page<SchedulingRuleDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<SchedulingRule> result = this.schedulingRuleRepository.search(pageable, searchable);
        return this.schedulingRuleConverter.toPageDTO(result);
    }

    @Override
    public SchedulingRuleDTO create(final SchedulingRuleDTO dto) {
        final SchedulingRule model = this.schedulingRuleConverter.toModel(dto);
        this.schedulingRuleRepository.save(model);
        return this.schedulingRuleConverter.toDTO(model);
    }

    @Override
    public SchedulingRuleDTO update(final Long id, final SchedulingRuleDTO dto) {

        SchedulingRule model;

        try {
            model = this.schedulingRuleRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.schedulingRuleConverter.toModel(dto);
        this.schedulingRuleRepository.save(model);

        return this.schedulingRuleConverter.toDTO(model);

    }

    @Override
    public SchedulingRuleDTO details(final Long id) {

        SchedulingRule model;

        try {
            model = this.schedulingRuleRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.schedulingRuleConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {

        try {
            this.schedulingRuleRepository.deleteById(id);
        } catch (final Exception e) {
            throw new UnitedException("规则正在执行中，不能删除！");
        }

    }

}
